package com.andyage.admin.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.andyage.admin.dao.AdItemMapper;
import com.andyage.admin.dao.AdLogMapper;
import com.andyage.admin.dao.AdProjectMapper;
import com.andyage.admin.service.IAdProjectService;
import com.andyage.bce.Sample;
import com.andyage.bce.UploadFile;
import com.andyage.util.HttpUtil;
import com.andyage.util.JsonContent;
import com.andyage.util.LogUtil;
import com.andyage.util.MyConfig;
import com.andyage.admin.bean.AdLogBean;
import com.andyage.admin.bean.AditemBean;
import com.andyage.admin.bean.AdprojectBean;

import net.sf.json.JSONObject;
@Service
public class AdProjectServiceImpl implements IAdProjectService {
	public  String CONTENT_TYPE="text/html;charset=utf-8";
	@Resource
	private  AdProjectMapper  adprojectMapper;
	@Resource
	private AdLogMapper adlogMapper;
	@Resource
	private AdItemMapper aditemMapper;
	@Override
	public void queryAdProject(HttpServletRequest request, HttpServletResponse response,AdprojectBean adProject) throws Exception {
		List<AdprojectBean> list=adprojectMapper.queryAdProject(adProject);
		int total=adprojectMapper.queryAdProjectCount(adProject);
		String json=JsonContent.getJsonArray(list, total);
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

	@Override
	public void addAdProject(HttpServletRequest request, HttpServletResponse response,AdprojectBean adProject) throws Exception {
		String path=request.getSession().getServletContext().getRealPath("/projectfile/");//获取路径
		
		String filename=adProject.getProjectId()+".project";
		String newpath=path+filename;
		System.out.println(path+filename);
		File file=new File(newpath);
		if(!file.getParentFile().exists()){
			file.getParentFile().mkdir();
		}
		if(!file.exists()){
			file.createNewFile();
		}else{
			file.delete();
		}
		//JSONObject jsonObject=new JSONObject();
		String projectContent=request.getParameter("projectContent");
		//jsonObject.accumulate("projectContent", projectContent);
		System.out.println(projectContent);
		byte[] b=projectContent.getBytes("utf-8");
		OutputStream os=new FileOutputStream(file);
		os.write(b);
		os.close();
		UploadFile upload=new UploadFile();
		upload.fileUploadObject(Sample.createBosClient(), MyConfig.BUCKET_NAME, filename,newpath);
		//百度bos返回的存储地址
		String bceURL=upload.generatePresignedUrl(Sample.createBosClient(), MyConfig.BUCKET_NAME, filename, -1);
		//请求地址返回的数据
		String result =	HttpUtil.sendPost(MyConfig.NORMAL, "projectId="+adProject.getProjectId());
		 //转成json对象
		JSONObject obj = JSONObject.fromObject(result);
		  JSONObject json = new JSONObject();
		if((int)obj.get("status")==1){
			adProject.setProjectContentUrl(bceURL);
			int flag=adprojectMapper.addAdProject(adProject);
			AdLogBean adlog = LogUtil.addLog((String)request.getSession().getAttribute("AccountId"), "新增项目", request.getRequestURI(),adProject.getProjectTitle());
			adlogMapper.insertLog(adlog);//写入日志
			if (flag!=0) {
				json.accumulate("result", 1);
				json.accumulate("tips", "新增成功");
			} else {
				json.accumulate("result", 0);
				json.accumulate("tips", "新增失败");
			}
		}else{
			json.accumulate("result", 0);
			json.accumulate("tips", "微信连接创建失败");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

	@Override
	public void updateAdProject(HttpServletRequest request, HttpServletResponse response,AdprojectBean adProject) throws Exception {
		String path=request.getSession().getServletContext().getRealPath("/projectfile/");//获取路径
		String filename=adProject.getProjectId()+".project";
		String newpath=path+filename;
		System.out.println(path+filename);
		File file=new File(newpath);
		if(!file.getParentFile().exists()){
			file.getParentFile().mkdir();
		}else if(!file.exists()){
			file.createNewFile();
		}else{
			file.delete();
		}
		//JSONObject jsonObject=new JSONObject();
		String projectContent=request.getParameter("projectContent");
		//jsonObject.accumulate("projectContent", projectContent);
		System.out.println(projectContent);
		byte[] b=projectContent.getBytes("utf-8");
		OutputStream os=new FileOutputStream(file);
		os.write(b);
		os.close();
		UploadFile upload=new UploadFile();
		upload.fileUploadObject(Sample.createBosClient(), MyConfig.BUCKET_NAME, filename,newpath);
		String bceURL=upload.generatePresignedUrl(Sample.createBosClient(), MyConfig.BUCKET_NAME, filename, -1);
		adProject.setProjectContentUrl(bceURL);
		int flag=adprojectMapper.updateAdProject(adProject);
		AdLogBean adlog = LogUtil.addLog((String)request.getSession().getAttribute("AccountId"), "修改项目内容", request.getRequestURI(),adProject.getProjectTitle());
		adlogMapper.insertLog(adlog);
		JSONObject json = new JSONObject();
		if (flag!=0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "修改成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "修改失败");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
		
	}

	@Override
	public void deleteAdProject(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//AdLogBean adlog = LogUtil.addLog((String)request.getSession().getAttribute("AccountId"), "删除项目", request.getRequestURI(),);
		//adlogMapper.insertLog(adlog);
		int flag=adprojectMapper.deleteAdProject(request.getParameter("projectId"));
		JSONObject json = new JSONObject();
		if (flag!=0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "删除成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "删除失败");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

	@Override
	public void updateProjectPublish(AdprojectBean adProject, HttpServletResponse response,HttpServletRequest request) throws Exception {
		int flag=adprojectMapper.updateProjectPublish(adProject);
		AdLogBean adlog = LogUtil.addLog((String)request.getSession().getAttribute("AccountId"), "项目发布", request.getRequestURI(),adProject.getProjectTitle());
		adlogMapper.insertLog(adlog);
		AditemBean aditem = new AditemBean();
		//更改回报状态，发布后更改为未满状态 0
		aditem.setItemStatus(0);
		aditem.setItemProjectId(adProject.getProjectId());
		aditemMapper.updateAditem(aditem);
		JSONObject json = new JSONObject();
		if (flag!=0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "发布成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "发布失败");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

	@Override
	public void queryProjectByStatus(AdprojectBean adProject, HttpServletResponse response) throws Exception {
		List<AdprojectBean> list = adprojectMapper.queryProjectByStatus(adProject);
		int total=adprojectMapper.queryProjectByStatusCount(adProject);
		String json=JsonContent.getJsonArray(list, total);
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

	@Override
	public void updateProjectByProjectStatus(HttpServletRequest request,AdprojectBean adProject, HttpServletResponse response) throws Exception {
		AditemBean aditem = new AditemBean();
		aditem.setItemProjectId(adProject.getProjectId());
		aditem.setItemStatus(1);//状态1,为设置回报已满，不可再继续支付
		aditemMapper.updateAditem(aditem);
		AdLogBean adlog = LogUtil.addLog((String)request.getSession().getAttribute("AccountId"), "项目下架", request.getRequestURI(),adProject.getProjectTitle());
		adlogMapper.insertLog(adlog);//写入日志
		adProject.setProjectStatus(3);//修改项目状态为下架状态
		int flag = adprojectMapper.updateProjectByProjectStatus(adProject);
		JSONObject json = new JSONObject();
		if (flag!=0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "下架成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "下架失败");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

}
